package com.fdb.efp.nls.service.impl.dw;

import com.fdb.basic.framework.core.util.DateTool;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.nls.service.vo.ImageBizInfoVO;
import com.fdb.efp.nls.service.domain.TxtFileLoadBean;
import com.fdb.efp.nls.service.domain.UpLoadFileTempBean;
import com.fdb.efp.nls.service.facade.ImageBizInfoService;
import com.fdb.efp.nls.service.facade.dw.ImageBizInfoEdocService;
import com.fdb.efp.nls.service.util.TxtFileLoadPlugin;
import com.fdb.efp.nls.service.facade.AccoutErroTempService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * 影像业务信息表
 * 
 * @author weijj
 * @since 2019年6月3日 下午8:30:08
 * @version 1.0
 */
@Service("imageBizInfoEdocService")
public class ImageBizInfoEdocServiceImpl implements ImageBizInfoEdocService {

	private static Logger logger = LoggerFactory.getLogger(ImageBizInfoEdocServiceImpl.class);

	@Autowired
	@Qualifier("imageBizInfoService")
	private ImageBizInfoService imageBizInfoService;// 影像业务信息表

	@Autowired
	@Qualifier("accoutErroTempService")
	private AccoutErroTempService accoutErroTempService;// 错误信息

	private boolean result = false;

	// 新增或者修改的影像业务信息表
	int num = 0;

	@Override
	public boolean batchUpdateImageBizInfo(String imageBizInfoUrl) throws Exception {
		// TODO Auto-generated method stub

		result = false;

		long startTime = System.currentTimeMillis();
		logger.info("同步过来的存量影像信息excel数据修改影像业务信息表：["
				+ DateTool.formatDate(new Date(startTime), DateTool.PATTERN_DATETIME) + "]， 处理开始！");
		TxtFileLoadPlugin txtFileLoadPlugin = new TxtFileLoadPlugin(imageBizInfoUrl, "\u0002", "UTF-8", 0, 10000);
		List<TxtFileLoadBean> testList = txtFileLoadPlugin.run(new UpLoadFileTempBean());
		dueData(testList);
		if (testList.size() != 0) {
			while (!testList.get(testList.size() - 1).isReadedComplete) {
				txtFileLoadPlugin.setFirstRead(false);
				testList = txtFileLoadPlugin.run(new UpLoadFileTempBean());
				if (testList.size() == 0) {
					break;
				}

				dueData(testList);

			}
		}
		long endTime = System.currentTimeMillis();
		logger.info("同步过来的存量影像信息excel数据修改影像业务信息表：[" + DateTool.formatDate(new Date(endTime), DateTool.PATTERN_DATETIME)
				+ "]， 处理结束！");
		logger.info("SERIOUS，耗时：[" + (endTime - startTime) / 1000.0 + "]秒，处理结果[" + result + "]");

		return result;
	}

	public void dueData(List<TxtFileLoadBean> testList) {
		try {
			UpLoadFileTempBean upLoadFileTempBean = new UpLoadFileTempBean();

			// 需要新增或者修改的影像业务信息表
			List<ImageBizInfoVO> list = null;
			list = new ArrayList<ImageBizInfoVO>();
			for (int i = 0; i < testList.size(); i++) {

				upLoadFileTempBean = (UpLoadFileTempBean) testList.get(i);
				if (upLoadFileTempBean.getImageType1() != null || upLoadFileTempBean.getImageType2() != null
						|| upLoadFileTempBean.getImageType3() != null) {
					// 影像业务信息表
					ImageBizInfoVO imageBizInfoVO = new ImageBizInfoVO();
					// 授信申请流水号
					imageBizInfoVO.setApplySeq(upLoadFileTempBean.getApplySeq());
					// 产品id 01010001满易贷 01010002尊享贷
					if ("度小满联合贷满易贷".equals(upLoadFileTempBean.getPrdName()))
						imageBizInfoVO.setPrdId("01010001");
					else if ("度小满联合贷尊享贷".equals(upLoadFileTempBean.getPrdName())) {
						imageBizInfoVO.setPrdId("01010002");
					}
					// 产品代码 01010001满易贷 01010002尊享贷
					if ("度小满联合贷满易贷".equals(upLoadFileTempBean.getPrdName()))
						imageBizInfoVO.setPrdCode("01010001");
					else if ("度小满联合贷尊享贷".equals(upLoadFileTempBean.getPrdName())) {
						imageBizInfoVO.setPrdCode("01010002");
					}
					// 产品名称 01010001满易贷 01010002尊享贷
					if ("度小满联合贷满易贷".equals(upLoadFileTempBean.getPrdName()))
						imageBizInfoVO.setPrdName("满意贷");
					else if ("度小满联合贷尊享贷".equals(upLoadFileTempBean.getPrdName())) {
						imageBizInfoVO.setPrdName("尊享贷");
					}
					// 客户号
					imageBizInfoVO.setCusId("");
					// 客户名称
					imageBizInfoVO.setCusName(upLoadFileTempBean.getCusName());
					// 影像文件接收
					imageBizInfoVO.setSoltImageAccept("1");
					// 影像文件加签
					imageBizInfoVO.setSoltImageCfca("");
					// 传送给影像系统
					imageBizInfoVO.setSoltImageThird1("");
					// 传送给业务联合方
					imageBizInfoVO.setSoltImageThird2("");
					// 创建时间
					if (!StringUtil.isNullorBank(upLoadFileTempBean.getCreateTime())) {
						imageBizInfoVO.setCreateTime(upLoadFileTempBean.getCreateTime());
					} else {
						imageBizInfoVO.setCreateTime(DateTool.getCurrentDateTime());
					}
					// 创建日期
					if (!StringUtil.isNullorBank(upLoadFileTempBean.getCreateTime())) {
						imageBizInfoVO.setCreateTime(formatDateTime19To10(upLoadFileTempBean.getCreateTime()));
					} else {
						imageBizInfoVO.setCreateDate(DateTool.getCurrentDate());
					}
					// 最近修改时间
					if (!StringUtil.isNullorBank(upLoadFileTempBean.getLastModifyTime())) {
						imageBizInfoVO.setLastModifyTime(upLoadFileTempBean.getLastModifyTime());
					} else {
						imageBizInfoVO.setLastModifyTime(DateTool.getCurrentDateTime());
					}

					list.add(imageBizInfoVO);
				}
			}
			if (list != null && !list.isEmpty()) {
				int updateSize = imageBizInfoService.insertOrUpdateImageBizInfoList(list);
				num += updateSize;
			}
			String num_msg = "【影像业务信息表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String formatDateTime19To10(String date) throws Exception {
		if (date == null || "".equals(date)) {
			return "";
		}
		if (date.length() != 10) {
			throw new Exception("时间：" + date + "格式不合规");
		}
		try {
			SimpleDateFormat sfl = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
			String dt = sf2.format(sfl.parse(date));
			return dt;
		} catch (Exception e) {
			throw new Exception("时间：" + date + "格式不合规");
		}
	}

}
